package com.openingedu.mba;

import net.umipay.android.GameParamInfo;
import net.umipay.android.UmiPaySDKManager;
import net.umipay.android.UmipaySDKStatusCode;
import net.umipay.android.interfaces.InitCallbackListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.openingedu.mba.common.AppSetting;
import com.openingedu.mba.common.CommConsts;
import com.openingedu.mba.screen.fragment.FragmentMessage;
import com.openingedu.mba.screen.fragment.LeftSlidingMenuFragment;
import com.openingedu.mba.service.BackendService;
import com.openingedu.mba.service.CommService;
import com.openingedu.mba.util.SettingsUtil;

/**
 * 初始界面
 * @author unidw
 *
 */
public class MainActivity extends SlidingFragmentActivity implements
		OnClickListener, InitCallbackListener {

	private static final String TAG = MainActivity.class.getSimpleName();
	protected SlidingMenu mSlidingMenu;
	private ImageButton ivTitleBtnLeft;
	private TextView ivTitleName;
	private SherlockFragment mFragmentContent;
	private Context mContext;
	private CommService commService;
	private LeftSlidingMenuFragment mFrag;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSlidingMenu();
		setContentView(R.layout.activity_main);
		mContext = this;
		initView();
		boolean initialed = SettingsUtil.queryBoolean(CommConsts.SettingsKey.INITIALED, false, mContext);
		// 第一次使用
		if (!initialed) {
			commService = new CommService();
			// 初始化本地消息
			commService.initLocalMessage(mContext);
			SettingsUtil.persist(CommConsts.SettingsKey.INITIALED, true, mContext);
		}
		// 初始化有米支付sdk参数
		initYoumiSDK();
		// 启动Service
		startBackendService();
	}

	private void startBackendService() {
		Intent intent = new Intent();
		intent.setClass(mContext, BackendService.class);
		startService(intent);
	}

	private void initView() {
		ivTitleBtnLeft = (ImageButton) this.findViewById(R.id.ivTitleBtnLeft);
		ivTitleBtnLeft.setOnClickListener(this);
		
		ivTitleName = (TextView) this.findViewById(R.id.ivTitleName);
	}

	private void initSlidingMenu() {
		mFragmentContent = new FragmentMessage();

		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragmentContent).commit();

		setBehindContentView(R.layout.main_left_layout);

		FragmentTransaction mFragementTransaction = getSupportFragmentManager().beginTransaction();
		mFrag = new LeftSlidingMenuFragment();
		mFragementTransaction.replace(R.id.main_left_fragment, mFrag);
		mFragementTransaction.commit();
		
		// customize the SlidingMenu
		mSlidingMenu = getSlidingMenu();
		// 设置是左滑还是右滑，还是左右都可以滑，我这里只做了左滑
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		// 设置菜单宽度
		mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// 设置淡入淡出的比例
		mSlidingMenu.setFadeDegree(0.35f);
		// 设置手势模式
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 设置左菜单阴影图片
		mSlidingMenu.setShadowDrawable(R.drawable.shadow);
		// 设置滑动时菜单的是否淡入淡出
		mSlidingMenu.setFadeEnabled(true);
		// 设置滑动时拖拽效果
		mSlidingMenu.setBehindScrollScale(0.333f);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivTitleBtnLeft:
			mSlidingMenu.showMenu(true);
			break;
		default:
			break;
		}

	}

	/**
	 * 左侧菜单点击切换首页的内容
	 */
	public void switchContent(SherlockFragment fragment) {
		mFragmentContent = fragment;
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
		getSlidingMenu().showContent();
	}
	
	public LeftSlidingMenuFragment getSlidingMenuFg() {
		return mFrag;
	}
	
	/**
	 * 设置标题名字
	 * @param titleName 标题名字
	 */
	public void setTitleName(String titleName) {
		ivTitleName.setText(titleName);
	}

	/**
	 * 初始化友盟支付SDK
	 */
	private void initYoumiSDK() {
		// 初始化参数
		GameParamInfo gameParamInfo = new GameParamInfo();
		// 您的应用的AppId
		gameParamInfo.setAppId(CommConsts.APPID);
		// 您的应用的AppSecret
		gameParamInfo.setAppSecret(CommConsts.APPSECRET);

		// false 订单充值成功后是使用服务器通知 true 订单充值成功后使用客户端回调
		gameParamInfo.setSDKCallBack(false);
		// 调用sdk初始化接口
		UmiPaySDKManager.initSDK(this, gameParamInfo, this, null);
	}

	@Override
	public void onInitCallback(int code, String msg) {
		if (code == UmipaySDKStatusCode.SUCCESS) {
			// 初始化成功后，即可正常调用充值
			Log.i(TAG, "Umipay init success.");
			AppSetting.UMIPAY_INITIALIZED = true;
		} else if (code == UmipaySDKStatusCode.INIT_FAIL) {
			// 初始化失败，一般在这里提醒用户网络有问题，反馈，等等问题
			Log.i(TAG, "Umipay init failure.");
		}
	}

}
